<?php

// 积分商品

namespace app\console\model;

use think\Db;
use think\Model;
use think\Request;

class IntegralGoods extends Model
{

    // 模型事件
    protected static function init()
    {

        // 新增前
        self::event('before_insert', function ($data) {
            $data->surplus = 0;
        });

        // 写入前
        self::event('before_write', function ($data) {
            $data->picUrl = uploadImg();                                                             // 图片
            $data->thumb_img = show_thumb_pic($data->picUrl);                                         // 缩略图
            // $data->create_time = strtotime(Request::instance()->param('create_time'));               // 创建时间

            // 如果类型为优惠券
            $type = Request::instance()->param('type');
            if ($type == 3) {
                $data->title = Db::name('Coupon')->where('id', Request::instance()->param('coupon_id'))->value('title');
            }
        });


        // 删除之前
        self::event('before_delete', function ($data) {
            if ($data->coupon_id) {
                $find = Db::name('MemberCoupon')->where(['coupon_id' => $data->coupon_id, 'status' => 1])->select();
                if ($find) {
                    return false;
                }
            }

        });
    }
}